<?php
namespace app\common\process;

class Snapshot extends Base {
    public function product($id, $oid) {
        $model = loadModel('tysj\\Product');
    	$result = null;
        try {
            $model->startTrans();
        	$result = $this->product_ipl($id, $oid);
            $model->commit();
    	} catch(\Exception $e) {
    		$model->rollback();
    		$this->error($e->getMessage());
    	}
        return $result;
    }

    public function product_ipl($id, $oid) {
        $model = loadModel('tysj\\Product');
        $row = $model->with(['store'])->find($id);
        if(empty($row)) {
            $this->error($id . '产品不存在');
        }
        if($row) {
            $row = $row->append(['photo_url','photos_url', 'info_photos_url', 'ost_sale', 'specs_list', 'keyword']);
        }
        $row = $row->toArray();
        
        $spid = $this->save_product($row, $oid);
        $this->save_imgs($row, $spid);
        $this->save_product_specs($row, $spid);
        $this->save_keyword($row, $spid);
        $this->assign('id', $spid);
        return $this->fetch();
    }
 
    private function save_product($row, $oid) {
    	$snapshotProduct = loadModel('jy\\SnapshotProduct');
        $snapshotProduct->id = null;
    	$snapshotProduct->isUpdate(false)->save([
    		'sid'         => $row['sid'],
            'oid'         => $oid,
    		'store_name'  => $row['store']['name'],
    		'store_photo' => $row['store']['photo'],
    		'photo'       => $row['photo_url'],
    		'name'        => $row['name'],
    		'selling'     => $row['selling'],
    		'classify'    => $row['product_classify'],
            'osid'        => $row['osid'],
    		'ost_sale'    => $row['ost_sale']
    	]);
    	return $snapshotProduct->id;
    }

    private function save_imgs($row, $spid) {
    	$typeEnum = loadEnum('ProductImageType');
    	$picType = $typeEnum->code2key('pic');
    	$snapshotImage = loadModel('jy\\SnapshotImage');
    	$imgs = [];
    	$photosUrl = $row['photos_url'];
    	foreach($photosUrl as $k => $v) {
    		$imgs[] = [
    			'spid'   => $spid,
    			'type'   => $picType,
    			'url'    => $v
    		];
    	}
    	$snapshotImage->isUpdate(false)->saveAll($imgs);

    	$infoType = $typeEnum->code2key('info');
    	$snapshotImage = loadModel('jy\\SnapshotImage');
    	$imgs = [];
    	$photosUrl = $row['info_photos_url'];
    	foreach($photosUrl as $k => $v) {
    		$imgs[] = [
    			'spid'   => $spid,
    			'type'   => $infoType,
    			'url'    => $v
    		];
    	}
    	$snapshotImage->isUpdate(false)->saveAll($imgs);
    }

    private function save_product_specs($row, $spid) {
    	$snapshotProductSpecs = loadModel('jy\\SnapshotProductSpecs');
    	$specs = [];
    	$specsList = $row['specs_list'];
    	foreach ($specsList as $k => $v) {
    		$specs[] = [
    			'spid'                  => $spid,
                'psid'                  => $v['id'],
    			'name'                  => $v['name'],
    			'crowdid'               => $v['crowdid'],
    			'price'                 => $v['price'],
    			'commission'            => $v['commission'],
    			'inventory'             => $v['inventory'],
    			'min_purchase_quantity' => $v['min_purchase_quantity'],
    			'weight'                => $v['weight'],
    			'volume'                => $v['volume']
    		];
    	}
    	$snapshotProductSpecs->isUpdate(false)->saveAll($specs);
    }

    private function save_keyword($row, $spid) {
    	$snapshotProductKeyword = loadModel('jy\\SnapshotProductKeyword');
    	$keywordList = $row['keyword'];
    	$keywords = [];
    	foreach ($keywordList as $k => $v) {
    		$keywords[] = [
    			'spid' => $spid,
    			'name' => $v
    		];
    	}
    	$snapshotProductKeyword->isUpdate(false)->saveAll($keywords);
    }


    //地址快照
    public function address($id) {
        $row = loadOnly('Model')->find('tyyh\\UserAddress', $id);
        $data = $row->hidden(['id', 'createtime', 'updatetime'])->toArray();
        $snapshotAddress = loadModel('jy\\SnapshotAddress');
        $snapshotAddress->allowField(true)->save($data);
        return $snapshotAddress->getLastInsId();
    }

    //删除快照地址
    public function del_address($id) {
        return loadModel('jy\\SnapshotAddress')->where('id', $id)->delete();
    }
}
